package com.itheima.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.OrderSettingQuery;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    /**
     * 1、form表单提交数据的格式 （name=jack&age=18&address=shangshou） 后端用对象接收不需要加注解
     * 2、页面传的是json数据，后端使用map 或者 pojo时 需要加@RequestBody
     * 3、基本类型 & 数组 & MultipartFile 只要保持页面的参数名称和controller方法形参一致就不用加@RequestParam
     * 4、List 不管名字一不一样 必须加@RequestParam
     *
     * @return
     */
    @Reference
    OrderSettingService orderSettingService;

    @RequestMapping("/setNumberByDate")
    public Result setNumberByDate(@RequestBody OrderSetting orderSetting) {
        orderSettingService.setNumberByDate(orderSetting);
        return Result.success("");
    }

    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile) {
        try {
            //list中每一个数组代表excel一行数据
            List<String[]> datas = POIUtils.readExcel(excelFile);
            //把datas转成List<OrderSetting>
            if (CollectionUtil.isNotEmpty(datas)) {
                List<OrderSetting> params = new ArrayList<>();
                for (String[] row : datas) {
                    if (row.length != 2) {
                        continue;
                    }

                    String dateStr = row[0];
                    String numberStr = row[1];
                    if (StrUtil.isBlank(dateStr) || StrUtil.isBlank(numberStr)) {
                        continue;
                    }

                    OrderSetting orderSetting = new OrderSetting(DateUtil.parse(dateStr, "yyyy/MM/dd"), Integer.valueOf(numberStr));
                    params.add(orderSetting);
                }
                //保存到数据库
                orderSettingService.batchSave(params);
            }

            return Result.success(MessageConstant.UPLOAD_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    @RequestMapping("/findByMonth")
    public Result findByMonth(@RequestBody OrderSettingQuery query) {
        //{"date":22,"mouth":0,"number":300,"reservations":300,},
        List<OrderSetting> orderSettings = orderSettingService.findByMonth(query.getDate());
//        List<Map> results = new ArrayList<>();
//        for (OrderSetting orderSetting : orderSettings) {
//            Map map = new HashMap<>();
//            map.put("date", orderSetting.getOrderDate().getDate());
//            map.put("month", orderSetting.getOrderDate().getMonth());
//            map.put("number", orderSetting.getNumber());
//            map.put("reservations", orderSetting.getReservations());
//            results.add(map);
//        }

        return Result.success(MessageConstant.GET_ORDERSETTING_SUCCESS, orderSettings);
    }

}
